package com.qiyou.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONArray;

import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.util.DBUtil;
import com.qiyou.util.LogUtil;

/**
 * 个人认证服务
 */
@WebServlet("/MyAttestationService")
public class MyAttestationService extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final Logger log = LogUtil
            .getLogger(MyAttestationService.class);

    public MyAttestationService() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");

        String userID = request.getParameter("id");
        String sql = "select `tb_scenic_spots`.scsID, `tb_scenic_spots`.scsName, `tb_scenic_spots`.scsPath, `temp_table`.attDate from `tb_scenic_spots`, (select `tb_place_attestation`.sceID, `tb_place_attestation`.attDate from `tb_place_attestation` where `tb_place_attestation`.userID=?) as `temp_table` where `tb_scenic_spots`.scsID=`temp_table`.sceID order by `temp_table`.attDate desc";

        Connection conn = DaoFactory.getDatabase().getConnection();
        List<Map<String, Object>> result = null;
        try {
            result = DBUtil.queryForMap(conn, sql, userID);
        } catch (SQLException e) {
            log.error(e);
        } finally {
            DBUtil.closeConnection(conn);
        }

        JSONArray questionjson = new JSONArray(result);
        String msg = questionjson.toString();
        PrintWriter out = response.getWriter();
        out.print(msg);
        out.flush();
        out.close();

        log.info("用户:" + userID + ",访问了个人认证信息:" + msg);
    }

}
